﻿using AutoMapper;
using Mysoft.Data;
using Mysoft.Domain.Basic;
using Mysoft.Dto;
using Mysoft.Entity;
using System.Collections;

namespace Mysoft.Domain
{
    /// <summary>
    /// 字典领域服务
    /// </summary>
    public class DictionaryDomainService: DomainBase
    {
        /// <summary>
        /// 字典实体服务
        /// </summary>
        private readonly EntityService<Sys_Dictionary> _entityService;
        /// <summary>
        /// autoMapper
        /// </summary>
        private readonly IMapper _mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        public DictionaryDomainService(EntityService<Sys_Dictionary> entityService, IMapper mapper) 
        {
            _entityService= entityService;
            _mapper= mapper;
        }
        /// <summary>
        /// 获取所有字典
        /// </summary>
        /// <returns></returns>
        public virtual async Task<List<Sys_Dictionary>> GetDictList()
        {
            var dictionaries = await _entityService.Instance.FindAsNoTrackingAsync(m => true);
            return dictionaries;
        }
        /// <summary>
        /// 获取所有字典
        /// </summary>
        /// <returns></returns>
        public virtual async Task<Hashtable> GetDict() 
        {
            Hashtable hashtable = new Hashtable();
            var dictionaries = _entityService.Instance.FindAsNoTracking(m => true).ToList();
            var headNodes = dictionaries.Where(m => m.PID == Guid.Empty).ToList();
            headNodes.ForEach(node =>
            {
                var lastNode = dictionaries.Where(m => m.PID == node.Id).ToList();
                hashtable.Add(node.Key, lastNode.Select(s => new { value = s.Value, label = s.Key }));
            });
            return hashtable;
        }
       

        /// <summary>
        /// 新增字典集合
        /// </summary>
        /// <param name="dictionaryDto"></param>
        /// <returns></returns>
        public virtual async Task<Guid> AddDict(DictionaryDto dictionary)
        {
            if (_entityService.Instance.Any(m=>m.Key == dictionary.Key)) 
            {
                BusinessException($"Key:{dictionary.Key}已存在！");
            }
            Sys_Dictionary sys_Dictionary = _mapper.Map<Sys_Dictionary>(dictionary);
            var info = await _entityService.Instance.AddAsync(sys_Dictionary);
            return info.Id;
        }
        /// <summary>
        /// 新增字典集合
        /// </summary>
        /// <param name="dictionaryDto"></param>
        /// <returns></returns>
        public virtual async Task AddDicts(List<DictionaryDto> dictionarys) 
        {
            List<Sys_Dictionary> sys_Dictionary = _mapper.Map<List<Sys_Dictionary>>(dictionarys);
            await _entityService.Instance.AddRangeAsync(sys_Dictionary);
        }
        /// <summary>
        /// 删除字典
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual void DeleteDict(Guid id) 
        {
            //校验是否有下级节点
            bool any = _entityService.Instance.Any(m => m.PID == id);
            if (any) 
            {
                BusinessException("存在依赖，请先删除下级节点！");
            }
            _entityService.Instance.Delete(m => m.Id == id);
        }
    }
}
